package com.xiaoyilin.controller;

import com.xiaoyilin.ack.RabbitConfirmCallback;
import com.xiaoyilin.ack.RabbitReturnCallback;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
public class TopicController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private RabbitConfirmCallback rabbitConfirmCallback;

    @Autowired
    private RabbitReturnCallback rabbitReturnCallback;

    @GetMapping("/topic/send")
    public void sender() {
        amqpTemplate.convertAndSend("my.topic.exchange", "my.topic.queues", "topic-queue-msg");
    }

    @GetMapping("/topic/ack/send")
    public void senderAck() {
        rabbitTemplate.setConfirmCallback(rabbitConfirmCallback);
        rabbitTemplate.setReturnCallback(rabbitReturnCallback);
        //CorrelationData 对象内部只有一个 id 属性，用来表示当前消息唯一性
        CorrelationData correlationData = new CorrelationData("msgId");
        this.rabbitTemplate.convertAndSend("my.topic.exchange", "my.topic.queue", "topic-queue-msg-ack" + LocalDateTime.now(), correlationData);
    }


}
